package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginPermissions;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategiesController {
   @Autowired
    private IRegionService regionService;
   @Autowired
   private IStrategyThemeService strategyThemeService;
   @Autowired
   private IStrategyService strategyService;
   @Autowired
   private IStrategyCommentService strategyCommentService;
   @Autowired
   private IStrategyStatisVORedisService strategyStatisVORedisService;


    @GetMapping("/detail")
    public Object strategies(String id){
        strategyStatisVORedisService.viewnumIncrease(id, 1);
        return JsonResult.success(strategyService.findById(id));
    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }

    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.findAll());
    }

    /**
     * 增加评论
     * @return
     */
    @LoginPermissions
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo){
        strategyCommentService.addComment(strategyComment, userInfo);
        strategyStatisVORedisService.replynumIncrease(strategyComment.getStrategyId(),1);
        return JsonResult.success();
    }

    /**
     * 评论分页
     * @return
     */
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        Page page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    /**
     * 评论点赞
     * @param cid 评论的id
     * @param sid 攻略id
     * @return
     */
    @LoginPermissions
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, String sid, @UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid, userInfo.getId());
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        StrategyStatisVO strategyStatisVO = strategyStatisVORedisService.statisVo(sid);
        return JsonResult.success(strategyStatisVO);
    }

    /**
     * 收藏操作
     * @param sid
     * @return
     */
    @LoginPermissions
    @PostMapping("/favor")
    public Object favor(String sid ,@UserParam UserInfo userInfo){

        boolean ret = strategyStatisVORedisService.favor(sid, userInfo.getId());
        return JsonResult.success(ret);
    }

    /**
     * 顶操作
     * @param sid
     * @param userInfo
     * @return
     */
    @LoginPermissions
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());

        return JsonResult.success(ret);
    }
}
